package com.jtpay.sdk.weight;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jtpay.sdk.R;

import java.util.List;

import apiUtils.models.PayPlatFormModel;

/**
 * Created by Administrator on 2017/9/1.
 */

public class PlatFromPop {
    private List<PayPlatFormModel> mPayPlatFormModels;
    private Context mContext;
    private PopupWindow mPopupWindow;
    private ListView mListView;
    private TextView mTextViewItem;

    public PlatFromPop(List<PayPlatFormModel> payPlatFormModels, Context context) {
        mPayPlatFormModels = payPlatFormModels;
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.pop_appname, null);
        mListView = (ListView) view.findViewById(R.id.listView);
        mListView.setAdapter(new PopAdapter());
        mListView.setFocusableInTouchMode(true);
        mListView.setFocusable(true);
        mPopupWindow = new PopupWindow(view, context.getResources().getDimensionPixelSize(R.dimen.popmenu_width), WindowManager.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListView.setOnItemClickListener(listener);
    }

    public void showAsDropDown(View parent, int offX) {
        int[] positon = new int[2];
        parent.getLocationInWindow(positon);
        mPopupWindow.showAtLocation(parent, Gravity.NO_GRAVITY, positon[0] + parent.getWidth() - mPopupWindow.getWidth() - offX, positon[1] + parent.getHeight());
    }

    public void dismiss() {
        mPopupWindow.dismiss();
    }

    private final class PopAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mPayPlatFormModels.size();
        }

        @Override
        public Object getItem(int position) {
            return mPayPlatFormModels.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.pop_appname_item, null);
            mTextViewItem = (TextView) convertView.findViewById(R.id.textView_item);
            mTextViewItem.setText(mPayPlatFormModels.get(position).getPtName());
            return convertView;
        }
    }
}